package com.ruoyi.web.controller.plan;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.OrdersCustomer;
import com.ruoyi.system.service.IOrdersCustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 交付订单Controller
 *
 * @author ruoyi
 * @date 2022-11-21
 */
@RestController
@Api(tags = "交付订单信息")
@RequestMapping("/orders/Customer")
public class OrdersCustomerController extends BaseController
{
    @Autowired
    private IOrdersCustomerService ordersCustomerService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询交付订单列表
     */
    @ApiOperation(value = "查询交付订单列表")
    @GetMapping("/list")
    public TableDataInfo list(@ApiParam(value = "交付订单参数", required = false) OrdersCustomer ordersCustomer)
    {
        startPage();
        List<OrdersCustomer> list = ordersCustomerService.selectOrdersCustomerList(ordersCustomer);
        return getDataTable(list);
    }

    /**
     * 查询交付订单下拉接口
     */
    @ApiOperation(value = "查询交付订单下拉接口")
    @GetMapping("/interface")
    public AjaxResult interfaces(@ApiParam(value = "交付订单参数", required = false) OrdersCustomer ordersCustomer)
    {
        List<OrdersCustomer> list = ordersCustomerService.selectOrdersCustomerList(ordersCustomer);
        return AjaxResult.success(list);
    }

    /**
     * 导出交付订单列表
     */
    @Log(title = "交付订单", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出交付订单列表")
    @GetMapping("/export")
    public AjaxResult export(HttpServletResponse response, @ApiParam(value = "交付订单参数", required = false) OrdersCustomer ordersCustomer)
    {
        List<OrdersCustomer> list = ordersCustomerService.selectOrdersCustomerList(ordersCustomer);
        ExcelUtil<OrdersCustomer> util = new ExcelUtil<OrdersCustomer>(OrdersCustomer.class);
        return util.exportExcel( list, "交付订单数据");
    }

    /**
     * 导入交付订单列表
     */
    @Log(title = "交付订单", businessType = BusinessType.IMPORT)
    @ApiOperation(value = "导入交付订单列表")
    @PostMapping("/importData")
    public AjaxResult importData(@ApiParam(value = "导入文件", required = true) MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<OrdersCustomer> util = new ExcelUtil<OrdersCustomer>(OrdersCustomer.class);
        List<OrdersCustomer> ordersCustomerList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = ordersCustomerService.importOrdersCustomer(ordersCustomerList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 导入模板
     */
    @ApiOperation(value = "导入模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        List<OrdersCustomer> list = ordersCustomerService.selectOrdersCustomerList(new OrdersCustomer());
        ExcelUtil<OrdersCustomer> util = new ExcelUtil<OrdersCustomer>(OrdersCustomer.class);
        return util.importTemplateExcel(list,"交付订单数据");
    }

    /**
     * 获取交付订单详细信息
     */
    @ApiOperation(value = "获取交付订单详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(value = "交付订单id", required = true) @PathVariable("id") Long id)
    {
        return AjaxResult.success(ordersCustomerService.selectOrdersCustomerById(id));
    }

    /**
     * 新增交付订单
     */
    @Log(title = "交付订单", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增交付订单")
    @PostMapping
    public AjaxResult add(@ApiParam(value = "交付订单实体类", required = true) @RequestBody OrdersCustomer OrdersCustomer)
    {

        return toAjax(ordersCustomerService.insertOrdersCustomer(OrdersCustomer));
    }

    /**
     * 修改交付订单
     */
    @Log(title = "交付订单", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改交付订单")
    @PutMapping
    public AjaxResult edit(@ApiParam(value = "交付订单实体类", required = true) @RequestBody OrdersCustomer ordersCustomer)
    {

        return toAjax(ordersCustomerService.updateOrdersCustomer(ordersCustomer));
    }

    /**
     * 删除交付订单
     */
    @Log(title = "交付订单", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除交付订单")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam(value = "交付订单id数组", required = true) @PathVariable Long[] ids)
    {
        return toAjax(ordersCustomerService.deleteOrdersCustomerByIds(ids));
    }
}
